<template>
  <div class="app-container">
    <el-form  @submit.native.prevent :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item   label="关键词" prop="key">
        <el-input
          v-model="queryParams.key"
          placeholder="请输入关键词 例如 Wig"
          value="shelf"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
       </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      
      
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="configList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="产品ID" align="center" prop="id" />
      <el-table-column label="P4P" align="center"   :formatter="p4p">  
      </el-table-column>
      <!-- <el-table-column label="下单" align="center"  :formatter="promotionTag" /> -->
      <el-table-column label="价格" align="center" prop="tradePrice.price" :show-overflow-tooltip="true" />
      <el-table-column label="起订量" align="center" prop="tradePrice.minOrder" :show-overflow-tooltip="true" />
      <!-- <el-table-column label="7天询盘数" align="center" prop="configKey" :show-overflow-tooltip="true" />
      <el-table-column label="30天询盘数" align="center" prop="configValue" />
      <el-table-column label="90天询盘数" align="center" prop="configType" :formatter="typeFormat" />
      <el-table-column label="90天TM数" align="center" prop="configType" :formatter="typeFormat" />
      <el-table-column label="类目半年询盘数" align="center" prop="configType" :formatter="typeFormat" />
      <el-table-column label="买家数" align="center" prop="remark" :show-overflow-tooltip="true" /> -->
      <el-table-column label="评价数" align="center" prop="reviews.reviewCount" :show-overflow-tooltip="true" />
      <el-table-column label="产品分" align="center" prop="reviews.productScore" :show-overflow-tooltip="true" />
      <el-table-column label="操作" align="center" >
        <template   slot-scope="{row}">            
           <el-link :href="'https://www.alibaba.com/product-detail/aa_'+row.id+'.html'" target="_blank" class="buttonText"  type="primary" :underline="false">访问产品</el-link>
       </template>  
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page-sizes="[20]"
      :page.sync="queryParams.pageNum"
      @pagination="getList"
    />


    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="产品信息" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="一行一个 ID/链接" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listConfig, addConfig, exportConfig, refreshCache } from "@/api/alibaba/key";

export default {
  name: "Key",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 参数表格数据
      configList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 类型数据字典
      typeOptions: [],
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        key: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        remark: [
          { required: true, message: "参数名称不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询参数列表 */
    getList() {
      this.loading = true;
      listConfig(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.configList = response.data;
          this.total = response.totalCount;
          this.loading = false;
        }
      );
    }, 
    fw(row, column) {
          return '<a href="http://suo.im/6wGm4u" target="_blank">1</a>';
    },
    p4p(row, column) {

        if(row.information.newAd){
        return "顶展";
       }else if(row.information.p4p){
        return "是";
       }else{
        return "否";
       }

    },
    promotionTag(row, column) {

    //  if(row.tag.promotionTag.name=="Ready to Ship"){
    //     return "RTS";
    //    }else{
    //     return "否";
    //    }

    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    }, 
    /** 搜索按钮操作 */
    handleQuery() {
      console.log("查询")
      this.queryParams.pageNum = 1;
      this.getList();
    },
   
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "批量查询";
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.configId)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
    
            addConfig(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
        }
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有产品信息?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportConfig(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>